class OrderItem < ApplicationRecord
    belongs_to :order,required: false
    belongs_to :car,required: false
    belongs_to :food_group,required: false
    belongs_to :food,required: false
    before_save :generate_single_price

    ADD = 0
    SUBSTRACT = 1

    def total_price
        single_price * quantity
    end  

    def food_name
        if self.food.present?
            self.food.name
        else
            self.food_group.name    
        end    
    end    

    def generate_single_price
        if self.single_price.blank?
          if self.food.present?                
            self.single_price = self.food.sell_price
          elsif self.food_group.present?
            self.single_price = self.food_group.price        
          end    
        end    
    end    
end
